class Node
attr_accessor :iter,:cols,:node_type,:subs,:children,:all_children,:parent,:index,:offset,:root,:expanded,:expandindex,:list,:nodepos
  def initialize(cols,type,list,parent = nil)
    @list = list
    @subs=[]
    if parent == nil
      @parent = self
      @offset = 0
      @root = self
      @all_children = []
    else
      @parent = parent
      @parent.children << self
      @offset = parent.offset
      @parent.subs << self if type == NODE_TYPE_EXPANDITEM
      @offset = @parent.offset + @parent.subs.length if type == NODE_TYPE_EXPANDITEM
      @root = @parent.root
      @root.all_children << self
    end
    @children = []
    @expanded = 0
    @cols = cols
    @iter = []
    @node_type = type
    for i in 0..cols
      @iter << nil
    end 
#     @list.add_node(self)
    @list = list
  end
  def get_children
    return @children
  end

  def unexpand(s)
# p s.list.indices.index(s.get_index)
# p s
# exit
    s.expandindex=s.list.indices.index(s.get_index)
# p s.expandindex
# exit
    s.children.each {|child|
# p s.list.nodepos
    
    if child!=self
      begin
        s.list.nodepos.delete_at(s.list.nodepos.index(child))
      rescue
      end
      while index=s.list.indices.index(child.get_index)
        s.list.indices.delete_at(index)
        s.list.array.delete_at(index)
        child.expanded = 0
        child.unexpand(child) if child.node_type == NODE_TYPE_EXPANDITEM
# p child.get_index
      end
    end
    }
# p "tree"
  end
  def reexpand(s,o=nil)
# p "re"
      begin
      i = s.list.nodepos.index(s.root)+s.root.all_children.index(s)+1
#       p i
#       p s.index
#       p s
#       exit
      rescue
      i = s.list.nodepos.index(s.parent)
#       p s.root == s
#       p s.index
      end
i = o if o != nil
    s.children::each {|child|
    i = i + 1
#     child.expanded = 1
    s.list.indices.insert(i,child.get_index)
    s.list.array.insert(i,child.iter)
#     s.list.reexpand child,i
    }
  end
  def set_index
# #     p @parent.children.length
    all = @parent.get_children.index(self)+1
    all = 0 if @node_type == NODE_TYPE_EXPANDITEM
#     if @parent != self
#     child = @root.all_children.index(@parent)+1 
#     else
#     child = 0
#     end
      i = @root.get_children.index(@parent)
      d = find_depth([],self)[0]
      last = @parent.get_children.index(self)
#       arr = @parent.get_index
      if @parent == self

      else
arr = []
nnode=self
node = self
br = false
until br
if node==node.parent
br = true
end
arr << node.parent.get_children.index(node) if node.parent.get_children.index(node) != nil
arr << @root.index[0] if node.parent.get_children.index(node) == nil
node = node.parent
# nnode = nnode.parent
end
#         arr << 0 if @parent.node_type == NODE_TYPE_EXPANDITEM
#         arr[arr.length-1] = arr.last+1 if arr.length > 1
        
#        else
# 
#        end
#         arr << 0 if @node_type == NODE_TYPE_EXPANDITEM
        @index = arr.reverse unless arr==[]
      end
      p @index
p d 
# exit
  end
  def find_depth ar,n,i=0
#     ar << i
    if n.parent == n
#       ar << i
    else
      find_depth(ar,n.parent,i+1)[1]
      i = i + 1
#       ar << i
    end
    return [i,ar]
  end
  def get_index
    return @index
  end
  def depth
    @index.length - 1
  end
end


# root depth child
# 000
#  001
#  002
#    011
#    012
#     020
#    012
#  003
#   01
#  0
# 
# 
# 000
# ---
#   010
#   -------
#   020
#   -------
#      021
#     -------
#       022
#       -------
# ---